package com.gin.no_gay_alliance.adpter.user;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;

import com.gin.nga.enums.SubForumType;
import com.gin.nga.enums.UserBuffType;
import com.gin.no_gay_alliance.activity.TopicListActivity;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.databinding.ItemUserBuffBinding;
import com.gin.no_gay_alliance.param.TopicListParam;
import com.gin.no_gay_alliance.viewmodel.UserBuffModel;

import java.util.List;
import java.util.Locale;

/**
 * 用户Buff适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/9/7 10:15
 **/
public class UserBuffAdapter extends BaseBindingRecyclerViewAdapter<ItemUserBuffBinding, UserBuffModel> implements View.OnClickListener {
    public static final String TAG = UserBuffAdapter.class.getSimpleName();
    private static final String DESC_TAG_AVTAR = "[头像改变]";
    private static final String DESC_TAG_MUTE = "[禁言]";

    public UserBuffAdapter(Context context) {
        super(context);
    }

    public UserBuffAdapter(@NonNull Context context, @NonNull List<UserBuffModel> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemUserBuffBinding> holder, @NonNull ItemUserBuffBinding b, int position, UserBuffModel item) {
        // 起止时间
        b.tvStart.setText(String.format("%s ~ ", item.start));
        b.tvEnd.setText(item.end);

        b.getRoot().setTag(item);
        b.getRoot().setOnClickListener(null);

        // 描述
        UserBuffType type = item.type;
        if (type == null) {
            Log.w(TAG, "无法识别的buff类型: " + item.typeId);
            b.tvDescription.setText(item.description);
            return;
        }
        switch (type) {

            // 版面或全版禁言
            case MUTED:
                if (item.forumName == null) {
                    b.tvDescription.setText(String.format(Locale.CHINA, "%s %s", DESC_TAG_MUTE, "在全版面被禁言"));
                } else {
                    b.tvDescription.setText(String.format(Locale.CHINA, "%s 在版面 %s(%d) 被禁言", DESC_TAG_MUTE, item.forumName, item.extraData));
                    // 点击跳转到该版面
                    b.getRoot().setOnClickListener(this);
                }
                break;
            //声望禁言
            case REPUTATION_MUTED:
                b.tvDescription.setText(String.format(Locale.CHINA, "%s %s", DESC_TAG_MUTE, item.description));
                break;
            case SHEEP:
                b.tvDescription.setText(String.format(Locale.CHINA, "%s %s", DESC_TAG_AVTAR, "头像被变成羊"));
                break;
            case EDIART:
                b.tvDescription.setText(String.format(Locale.CHINA, "%s %s", DESC_TAG_AVTAR, "头像被变成企鹅"));
                break;
            case AVATAR_CHANGED:
                b.tvDescription.setText(String.format(Locale.CHINA, "%s %s", DESC_TAG_AVTAR, item.description));
                break;
            case PERMISSION:
            case ANONY:
            case DISABLE_BUFF:
            default:
                b.tvDescription.setText(item.description);
        }
    }

    /**
     * 构造binding对象
     *
     * @param inflater
     * @return T
     */
    @NonNull
    @Override
    protected ItemUserBuffBinding buildBinding(LayoutInflater inflater) {
        return ItemUserBuffBinding.inflate(inflater);
    }

    @Override
    protected boolean useZebraStripe() {
        return false;
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        UserBuffModel item = (UserBuffModel) v.getTag();
        // 点击跳转到该版面
        TopicListActivity.startActivity(context, new TopicListParam(SubForumType.FORUM, item.extraData, item.forumName));
    }
}
